#include "stdint.h"
#include "tinymt32.h"
#include "DevicesTime.h"
#include "DevicesRandom.h"


static uint32_t st_uiRandSeed = 1234543;

void vSrandSoft(uint32_t uiRandSeed)
{
    st_uiRandSeed = uiRandSeed;
}

uint32_t uiRandSoft(void)
{
    static int8_t cCnt = 0;

    /* 增加混乱程度 */
    if(((cCnt++) % 17) == 0)
    {
        vSrandSoft(lTimebaseGet());
    }

    /* 线性同余 */
    st_uiRandSeed = st_uiRandSeed * 1103515245 + 12345;

    return st_uiRandSeed;
}


#define TINYMT32J_MAT1 0x8f7011eeU
#define TINYMT32J_MAT2 0xfc78ff1fU
#define TINYMT32J_TMAT 0x3793fdffU

static tinymt32_t st_typeRandMT = {.mat1 = TINYMT32J_MAT1, .mat2 = TINYMT32J_MAT2, .tmat = TINYMT32J_TMAT};

void vSrandMT(uint32_t uiRandSeed)
{
    tinymt32_init(&st_typeRandMT, uiRandSeed);
}

uint32_t uiRandMT(void)
{
    static int8_t cCnt = 0;

    /* 增加混乱程度 */
    if(((cCnt++) % 17) == 0)
    {
        tinymt32_init(&st_typeRandMT, lTimebaseGet());
    }

    return tinymt32_generate_uint32(&st_typeRandMT);
}
